package xiaozhi.Gui;

import xiaozhi.Class.Student;
import xiaozhi.Class.StudentManager;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class Gui extends  JFrame{
    // 默认表格模型
    private DefaultTableModel model = null;
    private JTable table = null;
    private JButton addBtn = null;

    int index=0;//限制表格属性重复田间



    public void run(final StudentManager studentManager) {

//        学生信息显示表格
        String[][] datas = {};
        String[] titles = { "学号", "姓名" ,"出生日期","性别"};
        model = new DefaultTableModel(datas, titles);
        table = new JTable(model);

//        Gui页面布局
        final JFrame Sys= new JFrame("学生信息管理系统");
        Sys.setSize(700, 700);
        Sys.setLocation(300, 100);
        Sys.setLayout((LayoutManager)null);
        JLabel Tile = new JLabel("请选择操作");
        Tile.setBounds(300, 50, 280, 30);
        JButton b1 = new JButton("添加学生信息");
        b1.setBounds(200, 100, 280, 30);
        JButton b2 = new JButton("查询学生信息");
        b2.setBounds(200, 150, 280, 30);
        JButton b3 = new JButton("删除学生信息");
        b3.setBounds(200, 200, 280, 30);
        JButton b4 = new JButton("修改学生信息");
        b4.setBounds(200, 250, 280, 30);
        JButton b5 = new JButton("查看所有学生信息");
        b5.setBounds(200, 300, 280, 30);
        JButton b6 = new JButton("退出系统");
        b6.setBounds(200, 350, 280, 30);
        table.setBounds(200, 400, 280, 300);
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int option = JOptionPane.showConfirmDialog(Sys, "是否要输入学生信息？");
                if (0 == option) {
                    Student student = new Student();
                    student.setID(Integer.parseInt(JOptionPane.showInputDialog(Sys, "输入学生学号")));
                    student.setName(JOptionPane.showInputDialog(Sys, "输入学生姓名"));
                    student.setBirDate(JOptionPane.showInputDialog(Sys, "输入学生生日"));
                    student.setGender(JOptionPane.showInputDialog(Sys, "输入学生性别"));
                    studentManager.addStudent(student);

                }

            }
        });
        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String name= JOptionPane.showInputDialog(Sys, "输入查询学生姓名");
                Student student=studentManager.getStudent(name);
                if (student != null) {
                    //model.addRow(new String[] {String.valueOf(student.getID()),student.getName(), student.getBirDate(), String.valueOf(student.isGender()?'男':'女')});
                    JOptionPane.showMessageDialog(Sys, student.toString(), "查询成功", 2);
                } else {
                    JOptionPane.showMessageDialog(Sys, "没有结果", "查询失败", 2);
                }

            }
        });
        b3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String name = JOptionPane.showInputDialog(Sys, "输入删除学生姓名");
                if (!studentManager.deleteStudent(name)) {
                    JOptionPane.showMessageDialog(Sys, "没有该学生，删除失败", "删除失败", 2);
                } else {

                    JOptionPane.showMessageDialog(Sys, "删除成功", "删除结果", 2);
                }

            }
        });

        b4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int option = JOptionPane.showConfirmDialog(Sys, "是否要修改学生信息？");
                if (0 == option) {
                    String name = JOptionPane.showInputDialog(Sys, "输入要修改学生姓名");
                    //查该学生是否存在
                    Student student=studentManager.getStudent(name);
                    //如果存在
                    if (student != null) {
                        student.setID(Integer.parseInt(JOptionPane.showInputDialog(Sys, "输入学生学号")));
                        student.setName(JOptionPane.showInputDialog(Sys, "输入学生姓名"));
                        student.setBirDate(JOptionPane.showInputDialog(Sys, "输入学生生日"));
                        student.setGender(JOptionPane.showInputDialog(Sys, "输入学生性别"));
                        studentManager.updateStudent(student);

                    } else {
                        JOptionPane.showMessageDialog(Sys, "没有结果", "查询失败", 2);
                    }



                }

            }
        });

        b5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (studentManager.isEmpty()) {
                    JOptionPane.showMessageDialog(Sys, "没有学生信息！", "查询失败", 2);
                } else {
                    List<Student> studentList = studentManager.getStudentList();
                    for (int i = 0; i < studentList.size(); i++) {
                        if(index<= studentList.size()-1){
                            model.addRow(new String[] {String.valueOf(studentList.get(i).getID()),studentList.get(i).getName(), studentList.get(i).getBirDate(), String.valueOf(studentList.get(i).isGender()?'男':'女')});
                            index++;//限制数据重复添加
                        }

                    }

                    //JOptionPane.showMessageDialog(Sys, studentList.toString(), "学生信息", 2);
                }

            }
        });
        //退出程序
        b6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                System.exit(0);
            }
        });

        Sys.add(b1);
        Sys.add(b2);
        Sys.add(b3);
        Sys.add(b4);
        Sys.add(b5);
        Sys.add(b6);
        Sys.add(table );
        Sys.add(Tile);
        Sys.setDefaultCloseOperation(3);
        Sys.setVisible(true);

    }


}
